package com.mystudy.navigation;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;


public class HomeActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private NavigationAdapter navigationAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);

        // Initialize ViewPager
        viewPager = findViewById(R.id.vp_home_pager);
        setupViewPager();

        // Initialize RecyclerView
        RecyclerView rvHomeNavigation = findViewById(R.id.rv_home_navigation);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,3);
        rvHomeNavigation.setLayoutManager(layoutManager);
        navigationAdapter = new NavigationAdapter();
        rvHomeNavigation.setAdapter(navigationAdapter);

        // Add sample navigation items
        navigationAdapter.addNavigationItem(new NavigationAdapter.MenuItem(R.drawable.home_home_selector, "Home"));
        navigationAdapter.addNavigationItem(new NavigationAdapter.MenuItem(R.drawable.home_message_selector, "Message"));
        navigationAdapter.addNavigationItem(new NavigationAdapter.MenuItem(R.drawable.home_me_selector, "Me"));
        // Set default item
        viewPager.setCurrentItem(2); // Set "Me" as the default fragment
        // Set click listener for navigation items
        navigationAdapter.setSelectedPosition(2);
        navigationAdapter.setOnNavigationItemClickListener(new NavigationAdapter.OnNavigationItemClickListener() {
            @Override
            public void onNavigationItemClick(int position) {
                viewPager.setCurrentItem(position, true);
                // Update navigation items' states
                navigationAdapter.setSelectedPosition(position);
            }
        });

    }

    private void setupViewPager() {
        // Create a FragmentPagerAdapter to manage the fragments
        FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                // Return the corresponding fragment based on the position
                switch (position) {
                    case 0:
                        return new HomeFragment();
                    case 1:
                        return new MessageFragment();
                    case 2:
                        return new MeFragment();
                    default:
                        return null;
                }
            }

            @Override
            public int getCount() {
                // Return the number of fragments
                return 3;
            }
        };

        // Set the pagerAdapter to the ViewPager
        viewPager.setAdapter(pagerAdapter);
    }
}